﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Collections;

namespace MPSync
{
    public partial class Form1 : Form
    {
        private delegate void SyncStartedDelegate(object sender, EventArgs e);
        private delegate void SyncUpdatedDelegate(object sender, SyncEventArgs e);
        private delegate void SyncCompletedDelegate(object sender, SyncEventArgs e);

        private SyncStartedDelegate mSyncStartedDelegate;
        private SyncUpdatedDelegate mSyncUpdatedDelegate;
        private SyncCompletedDelegate mSyncCompletedDelegate;

        private Sync mSync;

        private ArrayList mPendingSyncs;

        public Form1()
        {
            InitializeComponent();

            mSync = new Sync();

            mSync.SyncStarted += SyncStartedHandler;
            mSync.SyncUpdated += SyncUpdatedHandler;
            mSync.SyncCompleted += SyncCompletedHandler;

            mSyncStartedDelegate = SyncStartedHandler;
            mSyncUpdatedDelegate = SyncUpdatedHandler;
            mSyncCompletedDelegate = SyncCompletedHandler;

            mPendingSyncs = new ArrayList();
        }

        private void SyncStartedHandler(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(mSyncStartedDelegate, sender, e);
            }
            else
            {
                SyncEventArgs oe = (SyncEventArgs)e;
                mOutputList.Items.Add("Started - " + oe.mUpdateText);
                mOutputList.SelectedIndex = mOutputList.Items.Count - 1;
            }
        }

        private void SyncUpdatedHandler(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(mSyncUpdatedDelegate, sender, e);
            }
            else
            {
                SyncEventArgs oe = (SyncEventArgs)e;
                mOutputList.Items.Add(oe.mUpdateText);
                mOutputList.SelectedIndex = mOutputList.Items.Count-1;

                int value = Convert.ToInt32((Convert.ToDouble(oe.mCurrentFileNumber) / Convert.ToDouble(oe.mTotalFiles)) * 100.0);
                if (value > 0 && value < 101)
                {
                    mProgress.Value = Convert.ToInt32((Convert.ToDouble(oe.mCurrentFileNumber) / Convert.ToDouble(oe.mTotalFiles)) * 100.0);
                }
            }
        }

        private void SyncCompletedHandler(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(mSyncCompletedDelegate, sender, e);
            }
            else
            {
                SyncEventArgs oe = (SyncEventArgs)e;
                mOutputList.Items.Add("Completed - " + oe.mUpdateText);
                mOutputList.SelectedIndex = mOutputList.Items.Count - 1;

                if (mPendingSyncs.Count > 0)
                {
                    SyncItem s = (SyncItem)mPendingSyncs[mPendingSyncs.Count - 1];
                    mSync.SyncStart(s.getSourceDir(), s.getDestinationeDir());
                    mPendingSyncs.RemoveAt(mPendingSyncs.Count - 1);
                }
                else
                {
                    if (mStartMediaPortalCheck.Checked)
                    {
                        Thread.Sleep(1000);
                        System.Diagnostics.Process.Start(Properties.Settings.Default.MPMainDir + "\\MediaPortal.exe");
                    }

                    if (mExitAppAfterSyncEndsCheck.Checked)
                    {
                        Thread.Sleep(1000);
                        Environment.Exit(0);
                    }
                }
            }
        }

        private void mStartBtn_Click(object sender, EventArgs e)
        {
            StartSync();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            mMPMainDirTxt.Text = Properties.Settings.Default.MPMainDir;
            mMPAppDirTxt.Text = Properties.Settings.Default.MPAppDir;
            mBackupDirTxt.Text = Properties.Settings.Default.BackupDir;

            mSyncType.Text = Properties.Settings.Default.SyncType;
            mStartMediaPortalCheck.Checked = Properties.Settings.Default.StartMediaPortal;
            mExitAppAfterSyncEndsCheck.Checked = Properties.Settings.Default.ExitAppAfterSyncEnds;
            mStartSyncWhenAppStartsCheck.Checked = Properties.Settings.Default.StartSyncWhenAppStarts;
        }

        private void mClearBtn_Click(object sender, EventArgs e)
        {
            mOutputList.Items.Clear();
        }

        private void StartSync()
        {
            if (mSyncType.Text == "Master")
            {
                // Update the backup
                mSync.SyncStart(Properties.Settings.Default.MPMainDir, Properties.Settings.Default.BackupDir + "\\MPMainDir");

                SyncItem s = new SyncItem(Properties.Settings.Default.MPAppDir, Properties.Settings.Default.BackupDir + "\\MPAppDir");
                mPendingSyncs.Add(s);
            }
            else
            {
                // Restore the backup
                mSync.SyncStart(Properties.Settings.Default.BackupDir + "\\MPMainDir", Properties.Settings.Default.MPMainDir);

                SyncItem s = new SyncItem(Properties.Settings.Default.BackupDir + "\\MPAppDir", Properties.Settings.Default.MPAppDir);
                mPendingSyncs.Add(s);
            }
        }

        private void SaveSettings()
        {
            Properties.Settings.Default.SyncType = mSyncType.Text;
            Properties.Settings.Default.StartMediaPortal = mStartMediaPortalCheck.Checked;
            Properties.Settings.Default.StartSyncWhenAppStarts = mStartSyncWhenAppStartsCheck.Checked;
            Properties.Settings.Default.ExitAppAfterSyncEnds = mExitAppAfterSyncEndsCheck.Checked;
            Properties.Settings.Default.BackupDir = mBackupDirTxt.Text;
            Properties.Settings.Default.MPMainDir = mMPMainDirTxt.Text;
            Properties.Settings.Default.MPAppDir = mMPAppDirTxt.Text;

            Properties.Settings.Default.Save();
        }

        private void mSyncType_SelectedIndexChanged(object sender, EventArgs e)
        {
            mStartBtn.Enabled = true;

            switch (mSyncType.SelectedIndex)
            {
                case 0:
                    mStartMediaPortalCheck.Checked = true;
                    mStartSyncWhenAppStartsCheck.Checked = true;
                    mExitAppAfterSyncEndsCheck.Checked = true;
                    break;
                case 1:
                    mStartMediaPortalCheck.Checked = true;
                    mStartSyncWhenAppStartsCheck.Checked = true;
                    mExitAppAfterSyncEndsCheck.Checked = true;
                    break;
                case 2:
                    mStartMediaPortalCheck.Checked = false;
                    mStartSyncWhenAppStartsCheck.Checked = true;
                    mExitAppAfterSyncEndsCheck.Checked = true;
                    break;
            }

            SaveSettings();

            this.Focus();
        }

        private void mBackupDirModifyBtn_Click(object sender, EventArgs e)
        {
            mFolderBrowserDlg.ShowDialog();

            if (mFolderBrowserDlg.SelectedPath != "")
            {
                mBackupDirTxt.Text = mFolderBrowserDlg.SelectedPath;
            }

            SaveSettings();
        }

        private void mMPMainDirModifyBtn_Click(object sender, EventArgs e)
        {
            mFolderBrowserDlg.ShowDialog();

            if (mFolderBrowserDlg.SelectedPath != "")
            {
                mMPMainDirTxt.Text = mFolderBrowserDlg.SelectedPath;
            }

            SaveSettings();
        }

        private void mMPAppDirModifyBtn_Click(object sender, EventArgs e)
        {
            mFolderBrowserDlg.ShowDialog();

            if (mFolderBrowserDlg.SelectedPath != "")
            {
                mMPAppDirTxt.Text = mFolderBrowserDlg.SelectedPath;
            }

            SaveSettings();
        }

        private void mStartMediaPortalCheck_CheckedChanged(object sender, EventArgs e)
        {
            SaveSettings();
        }

        private void mExitAppAfterSyncEndsCheck_CheckedChanged(object sender, EventArgs e)
        {
            SaveSettings();
        }

        private void mStartSyncWhenAppStartsCheck_CheckedChanged(object sender, EventArgs e)
        {
            SaveSettings();
        }

        private void mTimer_Tick(object sender, EventArgs e)
        {
            if (mStartSyncWhenAppStartsCheck.Checked)
            {
                StartSync();
            }

            mTimer.Enabled = false;
        }
    }
}
